﻿
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class UIBattleLog : UIObject
{
    public GameObject gridRoot;
    public XIncubator incubator;
    protected override void Awake()
    {
        base.Awake();
        _sb = new StringBuilder();
    }

    private DataBattleMgr _dataBattleMgr;
    public void Bind(DataBattleMgr data)
    {
        _dataBattleMgr = data;
    }

    public void Unbind()
    {
        _dataBattleMgr = null;

        //删除
        for (int i = gridRoot.transform.childCount - 1; i > -1 ; i--)
        {
            var trans = gridRoot.transform.GetChild(i);
            XGameObject.Destroy(trans.gameObject);
        }
    }

    protected override void onVisible()
    {

        //有玩家做操作
        Facade.Instance.RegistEvent(EventID.ActionBattleReceive, OnHandleNotifyAction);
    }

    protected override void OnInvisible()
    {

        //有玩家做操作
        Facade.Instance.UnRegistEvent(EventID.ActionBattleReceive, OnHandleNotifyAction);
    }

    StringBuilder _sb;
    //数据处理中心
    private void OnHandleNotifyAction(System.Object rpcReq)
    {
        var rsp = rpcReq as notify_action_rsp ;
        XFactory.Instance.CreateInstanceAsync("UIBattleLogItem", incubator, (GameObject obj)
        =>{
            _sb.Clear();

            _sb.Append(System.DateTime.Now.Hour).Append(":").Append(System.DateTime.Now.Minute).Append(":").Append(System.DateTime.Now.Second);

            //from
            if (!string.IsNullOrEmpty(rsp.fromuid))
            {
                var data = _dataBattleMgr.GetDataPlayer(rsp.fromuid);
                if(data != null)
                    _sb.Append(" <color=#0000ff>").Append(data.GetUserName()).Append("</color>");
            }

            //actionType
            _sb.Append(ActionType.GetDes((int)rsp.type));

            //to
            if (!string.IsNullOrEmpty(rsp.touid))
            {
                if(rsp.fromuid == rsp.touid)
                {
                    _sb.Append(XLocalization.Get("自己"));
                }
                else
                {
                    var data = _dataBattleMgr.GetDataPlayer(rsp.touid);
                    if (data != null)
                        _sb.Append("<color=#0000ff>").Append(data.GetUserName()).Append("</color>");
                }

            }

            switch (rsp.type)
            {
                case ActionType.Close:
                    _sb.Append(XLocalization.Get("的骰子"));
                    break;
                case ActionType.Shake:
                    _sb.Append(XLocalization.Get("的骰子"));
                    break;
                case ActionType.Open:
                    _sb.Append(XLocalization.Get("的骰子"));
                    break;
                case ActionType.Glance:
                    _sb.Append(XLocalization.Get("的骰子"));
                    break;
                case ActionType.LookByMySelf:
                    _sb.Append(XLocalization.Get("的骰子") + XLocalization.Get("<color=#00ff00>他人不知骰盅里是什么</color>"));
                    break;
                default:
                    break;
            }

            //content
            if (rsp.type != ActionType.Shake && !string.IsNullOrEmpty(rsp.content))
            {
                _sb.Append(" ").Append(rsp.content);
            }

            //rander
            obj.transform.SetParent(gridRoot.transform, false);
            obj.transform.SetSiblingIndex(0);
            var text = obj.GetComponent<Text>();
            text.text = _sb.ToString();

            //太多了，删除一些
            if(gridRoot.transform.childCount > 50)
            {
                XGameObject.Destroy(gridRoot.transform.GetChild(gridRoot.transform.childCount - 1).gameObject);
            }
        });

        
    }
}
